<?php

class Tim {
	
	public $id;
	public $ime;
	public $grb;
	public $slika;
	
	public $roster;
	public $schedule;
	
	public $stats;
	
	public $brojPoena = 0;
	public $wins = 0;
	public $loses = 0;
	public $pts = 0;
	public $conceded = 0;
	
	// constructor
	
	public function __construct($i = 0) {
		// if ($i == 0) throw new Exception("Niste zadali ID Tima!");
		$this->id = $i;
		if ($i) {
			$n = $this->getfromdb($i);
			$this->ime = $n['ime'];
			$this->grb = $n['grb'];
			$this->slika = $n['slika'];
		}
	}
	
	// functions
	
	private function getfromdb($number) {
		$q = "SELECT id, ime, grb, slika FROM tim WHERE id = " . $number;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (!mysql_num_rows($r)) throw new Exception("Ne postoji tim za zadati ID!");
		return (mysql_fetch_array($r));
	}
	
	public function getRoster() {
		$q = "SELECT igrac FROM igra_za WHERE tim = " . $this->id;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r)) {
			while ($n = mysql_fetch_array($r)) {
				$this->roster[] = new Player($n['igrac']);
			}
		}
	}
	
	public function getSchedule() {
		$q = "SELECT id FROM utakmica WHERE domacin = " . $this->id . " OR gost = " . $this->id . " ORDER BY kolo ASC";
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (mysql_num_rows($r)) {
			while ($n = mysql_fetch_array($r)) {
				$this->schedule[] = new Match($n['id']);
			}
		}
	}
	
	public function getKapiten() {
		$q = "SELECT igrac FROM je_kapiten WHERE tim = " . $this->id;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		$n = mysql_fetch_array($r);
		return new Player($n['igrac']);
	}

	public function getMatchStats($match) {
		$helpTeam = new Tim($this->id);
		$helpTeam->getRoster();									// getting list of players
		$helpTeam->stats[$match] = new Statistics(0, $match);	// creating empty statistics
		
		foreach($helpTeam->roster as $player) {
			$player->getStats($match);							// getting each player stats
			
			$helpTeam->stats[$match]->ft += (int)$player->stats[$match]->ft;
			$helpTeam->stats[$match]->fg += (int)$player->stats[$match]->fg;
			$helpTeam->stats[$match]->threept += (int) $player->stats[$match]->threept;
			$helpTeam->stats[$match]->mft += (int)$player->stats[$match]->mft;
			$helpTeam->stats[$match]->mfg += (int)$player->stats[$match]->mfg;
			$helpTeam->stats[$match]->mtp += (int) $player->stats[$match]->mtp;
			$helpTeam->stats[$match]->orbd += (int)$player->stats[$match]->orbd;
			$helpTeam->stats[$match]->drbd += (int)$player->stats[$match]->drbd;
			$helpTeam->stats[$match]->blk += (int)$player->stats[$match]->blk;
			$helpTeam->stats[$match]->ast += (int)$player->stats[$match]->ast;
			$helpTeam->stats[$match]->stl += (int)$player->stats[$match]->stl;
			$helpTeam->stats[$match]->licne += (int)$player->stats[$match]->licne;
		}
		
		$helpTeam->stats[$match]->pts = $helpTeam->stats[$match]->calculatePts();
		$helpTeam->stats[$match]->totalRbd = $helpTeam->stats[$match]->calculateRbd();
		
		return $helpTeam->stats[$match];
	}
	
	public function getTotalStats() {
		$stats = new Statistics(0, 0);
		foreach ($this->roster as $igrac) {			
			$stats->licne += $igrac->totalStats->licne;
			$stats->ft += $igrac->totalStats->ft;
			$stats->fg += $igrac->totalStats->fg;
			$stats->threept += $igrac->totalStats->threept;
			$stats->mft += $igrac->totalStats->mft;
			$stats->mfg += $igrac->totalStats->mfg;
			$stats->mtp += $igrac->totalStats->mtp;
			$stats->orbd += $igrac->totalStats->orbd;
			$stats->drbd += $igrac->totalStats->drbd;
			$stats->blk += $igrac->totalStats->blk;
			$stats->ast += $igrac->totalStats->ast;
			$stats->stl += $igrac->totalStats->stl;
		}
		$stats->pts = $stats->calculatePts();
		$stats->totalRbd = $stats->calculateRbd();
		$stats->BLindex = $stats->calculateBLindex();
		
		return $stats;
	}
	
	public function getAverageStats() {
		$stats = $this->getTotalStats();
		$n = 0;
		if (count($this->schedule)) {
		foreach ($this->schedule as $mec) {
			if ($mec->odigrana) {
				$n++;
			}
		}
		}
		
		if ($n) {
			$stats->licne /= $n;
			$stats->ft /= $n;
			$stats->fg /= $n;
			$stats->threept /= $n;
			$stats->mft /= $n;
			$stats->mfg /= $n;
			$stats->mtp /= $n;
			$stats->orbd /= $n;
			$stats->drbd /= $n;
			$stats->blk /= $n;
			$stats->ast /= $n;
			$stats->stl /= $n;
			$stats->pts /= $n;
			$stats->totalRbd /= $n;
			$stats->BLindex /= $n;

			$stats->licne = round($stats->licne, 2);
			$stats->ft = round($stats->ft, 2);
			$stats->fg = round($stats->fg, 2);
			$stats->threept = round($stats->threept, 2);
			$stats->mft = round($stats->mft, 2);
			$stats->mfg = round($stats->mfg, 2);
			$stats->mtp = round($stats->mtp, 2);
			$stats->orbd = round($stats->orbd, 2);
			$stats->drbd = round($stats->drbd, 2);
			$stats->blk = round($stats->blk, 2);
			$stats->ast = round($stats->ast, 2);
			$stats->stl = round($stats->stl, 2);
			$stats->pts = round($stats->pts, 2);
			$stats->totalRbd = round($stats->totalRbd, 2);
			$stats->BLindex = round($stats->BLindex, 2);
		}
		else {
			$stats->licne = '-';
			$stats->ft = '-';
			$stats->fg = '-';
			$stats->threept = '-';
			$stats->orbd = '-';
			$stats->drbd = '-';
			$stats->blk = '-';
			$stats->ast = '-';
			$stats->stl = '-';
			$stats->pts = '-';
			$stats->totalRbd = '-';
			$stats->BLindex = '-';
		}
		return $stats;
	}
	
	// admin
	
	public function insertIntoDb() {
		$q = "INSERT INTO tim (ime, grb, slika) VALUES ('".$this->ime."', '".$this->grb."', '".$this->slika."')";
		$r = mysql_query("set names 'utf8'");
		if (mysql_query($q)) {
			echo "Novi tim je dodat u bazu podataka";
		}
	}
	
} // end of class Tim


?>